#ifdef LOCAL

#include <bits/stdc++.h>

using namespace std;
#endif

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    void flatten(TreeNode *root) {
        dfs(root);
        for (int num: preOrder) {
            TreeNode *node = new TreeNode(num);
            root->right = node;
        }
    }

    vector<int> preOrder;

    void dfs(TreeNode *root) {
        if (root == nullptr) return;
        preOrder.push_back(root->val);
        dfs(root->left);
        dfs(root->right);
    }
};

#ifdef LOCAL

int main() {
    freopen("../LeetCode/in.txt", "r", stdin);
    Solution solution;
}

#endif